#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;

string n;
int ans;

int main(){
	int m;
	cin>>n>>m;
	for(int j = n.size()-1;j>=0;j--) {
		if(n[j]>=48 && n[j]<=57){
			double tmp = n[j] - '0';
			ans+=tmp * pow(m,(double)(n.size()-1-j));
		}else{
			double tmp = n[j] - 'A' + 10;
			ans+=tmp * pow(m,(double)(n.size()-1-j));
		}
	}
	cout<<ans<<endl;
	return 0;
}
